<?
//-------------------------------------------------------------------
function get_max_round($val,$st = 1){

        if(($val/pow(10,$st)) <= 1) {

                $a =  (floor($val/pow(10,$st-1))*pow(10,$st-1) +pow(10,$st-1) );
                return $a;
        }
        else{
                $st++;
                return get_max_round($val,$st);
        }

}
//-------------------------------------------------------------------
function decode_params(){
        $buffer = explode("|||",$_SERVER['QUERY_STRING']);
        $buffer = $buffer[0];
        $chars = array(
                "_" => "=",
                "-" => "+"
                );
        return unserialize(@gzuncompress(base64_decode(strrev(strtr($buffer,$chars)))));
}
//-------------------------------------------------------------------
function generate_graph_stats($width,$height,$y_steps_count,$data,$data1,$data2,$data3,$data4,$details){

                        $im = @ImageCreate ($width,$height);

                        $color[background] = ImageColorAllocate ($im,255,255,255);
                        $color[border] = ImageColorAllocate ($im,0,0,0);
                        $color[grid_border] = ImageColorAllocate ($im,200,200,200);
                        $color[grid_background] = ImageColorAllocate ($im,255,255,200);
                        $color[graph_color] = ImageColorAllocate ($im,0,0,255);
                        $color[graph_color1] = ImageColorAllocate ($im,255,0,0);
			$color[graph_color2] = ImageColorAllocate ($im,0,200,0);
			$color[graph_color3] = ImageColorAllocate ($im,143,80,180);
			$color[graph_color4] = ImageColorAllocate ($im,80,255,168);


                        imagefilledrectangle ($im,0,0,$width,$height,$color[background]);
                        $elements_count = count($data);
                        imagefilledrectangle($im,30,10,$width-10,$height-30,$color[grid_background]);

//------------------------------------------------------------------------------
                        $data_coords = array();
                        foreach($data as $d) $buff_array[] = $d[count];
                        sort($buff_array);
                        $max_value = $buff_array[$elements_count-1];
                        $max_value = get_max_round($max_value);

                        if($max_value < $y_steps_count) $y_steps_count = $max_value;

                        $stepx = ($width-60)/$elements_count;
                        $stepy = ($height-40)/($y_steps_count+1);

                        $koef = (($height-40-(($height-40-($stepy*($y_steps_count)))))/$max_value);
//------------------------------------------------------------------------------
//GRID
//------------------------------------------------------------------------------
                        for($i=0;$i<$elements_count;$i++){
                                imageline($im,$i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2),10,$i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2),$height-30,$color[grid_border]);
                                $val = $data[$i][date];
                                imageString ($im, 1,$i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2),$height-20,$val,$color[border]);
                        }
                        for($i=0;$i<=$y_steps_count;$i++){
                                imageline($im,30,$i*$stepy+10+(($height-40-($stepy*($y_steps_count)))/2),$width-10,$i*$stepy+10+(($height-40-($stepy*($y_steps_count)))/2),$color[grid_border]);
                                $val = round(($y_steps_count-$i)*($max_value/$y_steps_count));
                                imageString ($im, 1,5,$i*$stepy+10+(($height-40-($stepy*($y_steps_count)))/2),$val,$color[border]);
                        }

//------------------------------------------------------------------------------
//GRAPH - 0
//------------------------------------------------------------------------------


                if(is_array($data)){
                                for($i=0;$i<$elements_count;$i++){
                                        $data_coords[$i][x] =  $i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2);
                                        $data_coords[$i][y] = ($height-30-($height-40-($stepy*($y_steps_count)))/2)-($koef*$data[$i][count]);
                                        $data_coords[$i][value] = $data[$i][count];
                                }
                                foreach($data_coords as $d){
                                        if(is_array($last)){
                                                imageline($im,$last[x]-1,$last[y]-1,$d[x]+1,$d[y]+1,$color[graph_color]);
                                                imageline($im,$last[x],$last[y],$d[x],$d[y],$color[graph_color]);
                                                imageline($im,$last[x]+1,$last[y]+1,$d[x]-1,$d[y]-1,$color[graph_color]);
                                        }
                                        if($details) imagefilledrectangle($im,$d[x]-1,$d[y]-1,$d[x]+1,$d[y]+1,$color[border]);
                                        if($last[y] > $d[y] ) $buff = 15;
                                        else $buff = 0;
                                        if($details) ImageString ($im,2,$d[x]-$buff,$d[y]-$buff,$d[value], $color[border]);
                                        $last = $d;
                                }
                }
//------------------------------------------------------------------------------
                        $data_coords = array();
                        $last = "";
//------------------------------------------------------------------------------
//GRAPH - 1
//------------------------------------------------------------------------------
                if(is_array($data1)){
                                for($i=0;$i<$elements_count;$i++){
                                        $data_coords[$i][x] =  $i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2);
                                        $data_coords[$i][y] = ($height-30-($height-40-($stepy*($y_steps_count)))/2)-($koef*$data1[$i][count]);
                                        $data_coords[$i][value] = $data1[$i][count];
                                }
                                foreach($data_coords as $d){
                                        if(is_array($last)){
                                                imageline($im,$last[x],$last[y],$d[x]-1,$d[y]-1,$color[graph_color1]);
                                                imageline($im,$last[x],$last[y],$d[x],$d[y],$color[graph_color1]);
                                                imageline($im,$last[x]+1,$last[y]+1,$d[x],$d[y],$color[graph_color1]);
                                        }
                                        if($details) imagefilledrectangle($im,$d[x]-1,$d[y]-1,$d[x]+1,$d[y]+1,$color[border]);
                                        if($last[y] > $d[y] ) $buff = 15;
                                        else $buff = 0;
                                        if($details) ImageString ($im,2,$d[x]-$buff,$d[y]-$buff,$d[value], $color[border]);
                                        $last = $d;
                                }
                }
//------------------------------------------------------------------------------
                        $data_coords = array();
                        $last = "";
//------------------------------------------------------------------------------
//GRAPH - 2
//------------------------------------------------------------------------------
                if(is_array($data2)){
                                for($i=0;$i<$elements_count;$i++){
                                        $data_coords[$i][x] =  $i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2);
                                        $data_coords[$i][y] = ($height-30-($height-40-($stepy*($y_steps_count)))/2)-($koef*$data2[$i][count]);
                                        $data_coords[$i][value] = $data2[$i][count];
                                }
                                foreach($data_coords as $d){
                                        if(is_array($last)){
                                                imageline($im,$last[x]+1,$last[y]+1,$d[x],$d[y],$color[graph_color2]);
                                                imageline($im,$last[x],$last[y],$d[x],$d[y],$color[graph_color2]);
                                                imageline($im,$last[x],$last[y],$d[x]-1,$d[y]-1,$color[graph_color2]);
                                        }
                                        if($details) imagefilledrectangle($im,$d[x]-1,$d[y]-1,$d[x]+1,$d[y]+1,$color[border]);
                                        if($last[y] > $d[y] ) $buff = 15;
                                        else $buff = 0;
                                        if($details) ImageString ($im,2,$d[x]-$buff,$d[y]-$buff,$d[value], $color[border]);
                                        $last = $d;
                                }
                }
//------------------------------------------------------------------------------
                        $data_coords = array();
                        $last = "";
//------------------------------------------------------------------------------
//GRAPH - 3
//------------------------------------------------------------------------------
                if(is_array($data3)){
                                for($i=0;$i<$elements_count;$i++){
                                        $data_coords[$i][x] =  $i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2);
                                        $data_coords[$i][y] = ($height-30-($height-40-($stepy*($y_steps_count)))/2)-($koef*$data3[$i][count]);
                                        $data_coords[$i][value] = $data3[$i][count];
                                }
                                foreach($data_coords as $d){
                                        if(is_array($last)){
                                                //imageline($im,$last[x]+1,$last[y]+1,$d[x],$d[y],$color[graph_color3]);
                                                imageline($im,$last[x],$last[y],$d[x],$d[y],$color[graph_color3]);
                                                //imageline($im,$last[x],$last[y],$d[x]-1,$d[y]-1,$color[graph_color3]);
                                        }
                                        if($details) imagefilledrectangle($im,$d[x]-1,$d[y]-1,$d[x]+1,$d[y]+1,$color[border]);
                                        if($last[y] > $d[y] ) $buff = 15;
                                        else $buff = 0;
                                        if($details) ImageString ($im,2,$d[x]-$buff,$d[y]-$buff,$d[value], $color[border]);
                                        $last = $d;
                                }
                }
//------------------------------------------------------------------------------
                        $data_coords = array();
                        $last = ""; 
//------------------------------------------------------------------------------
//GRAPH - 4
//------------------------------------------------------------------------------
                if(is_array($data4)){
                                for($i=0;$i<$elements_count;$i++){
                                        $data_coords[$i][x] =  $i*$stepx+30+(($width-40-($stepx*($elements_count-1)))/2);
                                        $data_coords[$i][y] = ($height-30-($height-40-($stepy*($y_steps_count)))/2)-($koef*$data4[$i][count]);
                                        $data_coords[$i][value] = $data4[$i][count];
                                }
                                foreach($data_coords as $d){
                                        if(is_array($last)){
                                                //imageline($im,$last[x]+1,$last[y]+1,$d[x],$d[y],$color[graph_color4]);
                                                imageline($im,$last[x],$last[y],$d[x],$d[y],$color[graph_color4]);
                                                //imageline($im,$last[x],$last[y],$d[x]-1,$d[y]-1,$color[graph_color4]);
                                        }
                                        if($details) imagefilledrectangle($im,$d[x]-1,$d[y]-1,$d[x]+1,$d[y]+1,$color[border]);
                                        if($last[y] > $d[y] ) $buff = 15;
                                        else $buff = 0;
                                        if($details) ImageString ($im,2,$d[x]-$buff,$d[y]-$buff,$d[value], $color[border]);
                                        $last = $d;
                                }
                }

//------------------------------------------------------------------------------
                        $data_coords = array();
                        $last = "";
//------------------------------------------------------------------------------



                        imagerectangle($im,30,10,$width-10,$height-30,$color[border]);
                        imagerectangle($im,0,0,$width-1,$height-1,$color[border]);

                        return $im;
        }


$p = decode_params();
//print_r($p);
//exit();
$im = generate_graph_stats($p[width],$p[height],$p[ycount],$p[data1],$p[data2],$p[data3],$p[data4],$p[data5],$p[details]);
header ("Content-type: image/png");
imagepng ($im);


?>